package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jx.dao.CatDao;
import com.jx.dao.ProductDao;
import com.jx.daoimpl.CatDaoImpl;
import com.jx.daoimpl.ProductDaoImpl;
import com.jx.model.Product;
import com.jx.service.ProService;
import com.jx.servicrimpl.ProServiceImpl;

public class UpdProController extends HttpServlet{
		//定义ProService类型的proservice引用指向ProServiceImpl对象，以调用sevice层方法
		ProService proservice = new ProServiceImpl();
		ProductDao prodao = new ProductDaoImpl();
		CatDao catdao = new CatDaoImpl();
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			// TODO Auto-generated method stub
			doPost(req, resp);
		}
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
			//设置字符集
			req.setCharacterEncoding("UTF8");
			resp.setCharacterEncoding("UTF8");
			//创建流对象
			PrintWriter pw = resp.getWriter();
			//创建session对象
			HttpSession session = req.getSession();

			session.setAttribute("category", catdao.showCatrgory(0, 50));
			if (req.getParameter("pname")==""||req.getParameter("pprice")==""||req.getParameter("pintroduce")==""||req.getParameter("ppicture")==""||req.getParameter("pcount")==""	) {
				pw.write("0");
			}else {
				//接收前台传来的数据
				String pname = req.getParameter("pname");
				double pprice = Double.parseDouble(req.getParameter("pprice"));
				String pintroduce = req.getParameter("pintroduce");
				String ppicture = req.getParameter("ppicture");
				int pcount =Integer.parseInt(req.getParameter("pcount"));
				String pmodel = req.getParameter("pmodel");
				String psize = req.getParameter("psize");
				String pcolor = req.getParameter("pcolor");
				int pstate = Integer.parseInt(req.getParameter("pstate"));
				String cname = req.getParameter("cname");	
				int pid = Integer.parseInt(req.getParameter("pid"));
				int cid = prodao.selectCidByCname(cname);
				//实例化产品对象
				Product p = new Product(pid,pname, pprice, pintroduce, ppicture, pcount, pmodel, psize, pcolor, pstate, cid);
				if (proservice.updProductService(p)) {
					pw.write("1");
				}				
			}

			pw.flush();
			pw.close();
		}
}
